(function () {
	const container = document.querySelector('.container');
	const preview = document.querySelector('.preview');

	const userSettings = JSON.parse(document.getElementById('vscode-blpviewer-data').getAttribute('data-settings'));
	const fileToLoad = userSettings.fileToLoad;
	console.log("filetoLoad", fileToLoad)

	const oReq = new XMLHttpRequest();
	oReq.open("GET", fileToLoad, true);
	oReq.responseType = "arraybuffer";

	oReq.onload = oEvent => {
		try {
			const arrayBuffer = oReq.response; // 注意：不是 oReq.responseText

			console.log("datadd", arrayBuffer);

			const blp = war3model.decodeBLP(arrayBuffer);

			console.log("dd", blp);

			clearPreview();
			container.classList.add('container_hidden');
			for (let i = 0; i < blp.mipmaps.length; ++i) {
				const imageData = war3model.getBLPImageData(blp, i);

				const canvas = document.createElement('canvas');
				canvas.width = imageData.width;
				canvas.height = imageData.height;
				const ctx = canvas.getContext('2d');
				ctx.putImageData(imageData, 0, 0);

				const previewItem = document.createElement('div');
				previewItem.className = 'preview-item';
				const previewLabel = document.createElement('div');
				previewLabel.className = 'preview-item__label';
				previewLabel.textContent = `${imageData.width}x${imageData.height}`;
				previewItem.appendChild(canvas);
				previewItem.appendChild(previewLabel);

				preview.appendChild(previewItem);
			}
		} catch (e) {
			showError(e)
		}

	};
	oReq.send(null);

	function clearPreview() {
		preview.innerHTML = '';
	}

	function showError(err) {
		clearPreview();
		container.classList.remove('container_hidden');
		console.error(err);
	}
}());